package com.caine.androidtask.ls28;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;

public class ExternalListener extends Activity {

	/** textView displays the status */
	private static final String STATUS = "status";

	/** textView displays the plugged */
	private static final String PLUGGED = "plugged";

	private static final String VOLTAGE = "voltage";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(mBroadcastReceiver);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		IntentFilter filter = new IntentFilter();

		filter.addAction(Intent.ACTION_BATTERY_CHANGED);

		/** Registration BroadcastReceiver */
		registerReceiver(mBroadcastReceiver, filter);
	}

	/** receive the various states of battery */
	private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {

				int status = intent.getIntExtra(STATUS, 0);

				int plugged = intent.getIntExtra(PLUGGED, 0);

				int voltage = intent.getIntExtra(VOLTAGE, 0);

				String statusString = "";

				switch (status) {

				case BatteryManager.BATTERY_STATUS_UNKNOWN:

					statusString = "unknown";

					break;

				case BatteryManager.BATTERY_STATUS_CHARGING:

					statusString = "charging";

					break;

				case BatteryManager.BATTERY_STATUS_DISCHARGING:

					statusString = "discharging";

					break;

				case BatteryManager.BATTERY_STATUS_NOT_CHARGING:

					statusString = "not charging";

					break;

				case BatteryManager.BATTERY_STATUS_FULL:

					statusString = "battery full";

					break;
				default:
					break;

				}

				String acString = "";

				switch (plugged) {

				case BatteryManager.BATTERY_PLUGGED_AC:

					acString = " BATTERY : plugged AC";

					break;

				case BatteryManager.BATTERY_PLUGGED_USB:

					acString = " BATTERY : plugged USB";

					break;
				default:
					acString = " BATTERY : Default";
					break;

				}

			}

		}

	};

}
